import sqlite3
import math
import os

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

def del_file(fp):
	if os.path.exists(fp):
		os.remove(fp)
	else:
		print('no such file:%s'%fp)

conn = sqlite3.connect('../app.db')
conn.row_factory = dict_factory
cursor = conn.cursor()



sql = 'select count(*) num from os_storage_duplicate_files'
cursor.execute(sql)
row = cursor.fetchone()

total = row.get('num', 0)
size = 10
total_page = math.ceil(total / size)

for i in range(0, total_page):
	offset = i * size
	sql = 'select * from os_storage_duplicate_files limit {},{}'.format(offset, size)
	cursor.execute(sql)
	rows = cursor.fetchall()
	for row in rows:
		fp = os.path.join(row.get('dir'), row.get('filename'))
		del_file(fp)


conn.close()